//////////// SHARE CONSTANTS ////////////////////
var TYPE = new Object();
TYPE.GET_DOM_FOR_CHECK = "GetDOMForCheck";
TYPE.GET_DOM_FOR_PROCESS = "GetDOMForProcess";
TYPE.ADD_REMARK_BETTING = "AddRemarkBettingData";
TYPE.REMOVE_REMARK_BETTING = "RemoveRemarkBettingData";
////////////////////////////////////////////////

//////////// LOCAL CONSTANTS ////////////////////

var CSS_ERROR_CLASS = "CE_Error";
var CSS_CHANGED_CLASS = "CE_Changed";
var PAGE_TITLE_BETTING = "♥♥♥";
var GlobalData = {};
var TIME_ADD_REMARK = 1000; //miliseconds
var SAI_SO = 0.02;
var oldPageTile = '';

//////////// END LOCAL CONSTANTS ////////////////////

chrome.runtime.onMessage.addListener(
        function(request, sender, sendResponse) {
            switch (request.Type) {
                case TYPE.GET_DOM_FOR_CHECK:
                    GetDOMValueForCheck(request.Data, sendResponse);
                    break;
                case TYPE.GET_DOM_FOR_PROCESS:
                    GetDOMValueForProcess(request.Data, sendResponse);
                    break;
                case TYPE.ADD_REMARK_BETTING:
                    if (GlobalData[request.Data.MatchId] && GlobalData[request.Data.MatchId].IntervalId) {
                        clearInterval(GlobalData[request.Data.MatchId].IntervalId);
                    }

                    GlobalData[request.Data.MatchId] = request.Data;
                    GlobalData[request.Data.MatchId].IntervalId = setInterval(AddRemark, TIME_ADD_REMARK, request.Data.MatchId);
                    break;
                case TYPE.REMOVE_REMARK_BETTING:
                    RemoveRemark(request.Data.MatchId);
                    clearInterval(GlobalData[request.Data.MatchId].IntervalId);
                    break;
            }
        });

function GetDOMValueForCheck(requestData, sendResponse) {
    var value = GetValue(requestData.SiteType, requestData.Filter);
    var data = {
        MatchId: requestData.MatchId,
        PairId: requestData.PairId,
        Key: requestData.Key,
        Value: value
    };
    sendResponse(data);
}

function GetValue(siteType, filter) {
    var result = "";
    var element;
    switch (siteType)
    {
        case "0": //marathonbet
            filter = "[data-selection-key='" + filter + "']";
            element = $(filter);
            if (element && element.length)
            {
                result = element.html();
            }
            break;
        case "1": //paddypower
            filter = "#" + filter;
            element = $(filter);
            if (element && element.length)
            {
                result = element.html();
            }
            break;
        case "2": //planetofbets
            filter = "#" + filter + " span";
            element = $(filter);
            if (element && element.length)
            {
                result = $.trim(element.text());
            }
            break;
        case "3": //parimatch
            var _filters = filter.split(' ');
            filter = "#" + _filters[0] + "  tbody.row1 td:eq(" + (parseInt(_filters[1]) - 1) + ")";
            element = $(filter);
            var _value = element.html();
            while (_value && !$.isNumeric(_value))
            {
                element = element.children().first();
                _value = element.html();
            }
            result = _value;
            break;
        case "4": //betinternet
            filter = "." + filter + " span";
            element = $(filter);
            if (element && element.length)
            {
                result = $.trim(element.html());
            }
            break;

        case "5": //favbet
            filter = "[oid='" + filter + "']";
            element = $(filter);
            if (element && element.length)
            {
                result = element.html();
            }
            break;
    }
    return result;
}

function GetDOMValueForProcess(rqstData, sendResponse) {
    var value = GetValue(rqstData.SiteType, rqstData.Key);
    var data = {
        MatchId: rqstData.MatchId,
        PairIndex: rqstData.PairIndex,
        SiteIndex: rqstData.SiteIndex,
        Value: value
    };
    sendResponse(data);
}

function AddRemark(matchId) {
    oldPageTile = document.title;
    document.title = PAGE_TITLE_BETTING;

    var value = "";
    var element;
    var filter = GlobalData[matchId].Key;
    switch (GlobalData[matchId].SiteType)
    {
        case "0": //marathonbet
            filter = "[data-selection-key='" + filter + "']";
            element = $(filter);
            if (element && element.length)
            {
                value = element.html();
            }
            break;
        case "1": //paddypower
            filter = "#" + filter;
            element = $(filter);
            if (element && element.length)
            {
                value = element.html();
            }
            break;
        case "2": //planetofbets
            filter = "#" + filter + " span";
            element = $(filter);
            if (element && element.length)
            {
                value = $.trim(element.text());
            }
            break;
        case "3": //parimatch
            var _filters = filter.split(' ');
            filter = "#" + _filters[0] + "  tbody.row1 td:eq(" + (parseInt(_filters[1]) - 1) + ")";
            element = $(filter);
            var _value = element.html();
            while (_value && !$.isNumeric(_value))
            {
                element = element.children().first();
                _value = element.html();
            }
            value = _value;
            break;
        case "4": //betinternet
            filter = "." + filter + " span";
            element = $(filter);
            if (element && element.length)
            {
                value = $.trim(element.html());
            }
            break;
        case "5": //favbet
            filter = "[oid='" + filter + "']";
            element = $(filter);
            if (element && element.length)
            {
                value = element.html();
            }
            break;
    }

    value = $.trim(value);
    if (element && element.length && value)
    {
        value = parseFloat(value);
        var bettingValue = parseFloat(GlobalData[matchId].Value);
        if (value >= bettingValue - SAI_SO && value <= bettingValue + SAI_SO) {
            $(element).addClass(CSS_ERROR_CLASS);
        }
        else
        {
            $(element).addClass(CSS_CHANGED_CLASS);
        }
    }
}

function RemoveRemark(matchId) {
    document.title = oldPageTile;
    var element;
    var filter = GlobalData[matchId].Key;
    switch (GlobalData[matchId].SiteType)
    {
        case "0": //marathonbet
            filter = "[data-selection-key='" + filter + "']";
            $(filter).removeClass(CSS_ERROR_CLASS);
            $(filter).removeClass(CSS_CHANGED_CLASS);
            break;
        case "1": //paddypower
            filter = "#" + filter;
            $(filter).removeClass(CSS_ERROR_CLASS);
            $(filter).removeClass(CSS_CHANGED_CLASS);
            break;
        case "2": //planetofbets
            filter = "#" + filter + " span";
            $(filter).removeClass(CSS_ERROR_CLASS);
            $(filter).removeClass(CSS_CHANGED_CLASS);
            break;
        case "3": //parimatch
            var _filters = filter.split(' ');
            filter = "#" + _filters[0] + "  tbody.row1 td:eq(" + (parseInt(_filters[1]) - 1) + ")";
            element = $(filter);
            element.removeClass(CSS_ERROR_CLASS);
            element.removeClass(CSS_CHANGED_CLASS);
            var _value = element.html();
            while (_value && !$.isNumeric(_value))
            {
                element = element.children().first();
                _value = element.html();
                element.removeClass(CSS_ERROR_CLASS);
                element.removeClass(CSS_CHANGED_CLASS);
            }
            break;
        case "4": //betinternet
            filter = "." + filter + " span";
            $(filter).removeClass(CSS_ERROR_CLASS);
            $(filter).removeClass(CSS_CHANGED_CLASS);
            break;
        case "5": //favbet
            filter = "[oid='" + filter + "']";
            $(filter).removeClass(CSS_ERROR_CLASS);
            $(filter).removeClass(CSS_CHANGED_CLASS);
            break;
    }
}